package com.firebird.fekpsl.socket.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketService {

	private ServerSocket serverSocket;
	private ExecutorService eService=null;//线程池管理
	private Map<String, Socket> cliectMap;//socket连接对象集合
	private Map<String,Map<String, Participator>> roomMap;//房间对象集合
	private Map<String,Map<String, Socket>> roomPrepare;//房间已准备对象集合
	private Map<String,Map<String, String>> roomResultMap;//每个房间对应的每个人的答题结果
	private Map<String,Map<String,Map<String, String> >> roomIndexResultMap;//房间的每一道题目的答案Map<roomId, Map<index,Map<userId, answer> >>
	private Map<String,Map<String, String>> roomFinish;//每个房间对应的每个人的是否结束答题Map<roomId,Map<userId, finish>>

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("开启服务");
		new SocketService();
	}

	public SocketService(){
		try {
			serverSocket=new ServerSocket(8999);
			eService=Executors.newCachedThreadPool();
			cliectMap=new HashMap<String, Socket>();
			roomMap=new HashMap<String,Map<String, Participator>>();
			roomPrepare=new HashMap<String,Map<String, Socket>>();
			roomResultMap=new HashMap<String,Map<String, String>>();
			roomIndexResultMap=new HashMap<String,Map<String,Map<String, String> >>();
			roomFinish=new HashMap<String,Map<String, String>>();
			Socket socket=null;
			while(true){
				socket=serverSocket.accept();
				cliectMap.put(socket.getInetAddress().toString(), socket);
				System.out.println(socket.toString());
				eService.execute(new ServerRunnable(socket, this));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void remove(String address){
		cliectMap.remove(address);
	}

	public int clientList(){
		return cliectMap.size();
	}

	/**
	 * 将新的成员加入房间
	 * @param roomId
	 * @param userId
	 * @param socket
	 */
	public void addToRoom(String roomId,String userId,Participator socket){
		Map<String, Participator> map=roomMap.get(roomId);
		if(map==null){
			Map<String, Participator> rooMap=new HashMap<>();
			rooMap.put(userId, socket);
			roomMap.put(roomId, rooMap);
		}else{
			map.put(userId, socket);
			roomMap.put(roomId, map);
		}
	}
	/**
	 * 获取房间所有成员
	 * @param roomId
	 * @return
	 */
	public Map<String, Participator> getRoom(String roomId){
		return roomMap.get(roomId);
	}

	/**
	 * 从房间列表中移除当前的房间
	 * @param roomId
	 */
	public void destroyRoom(String roomId){
		destroyResult(roomId);
	}

	/**
	 * 成员从房间离开
	 */
	public void quitFroRoom(String roomId,String userId){
		Map<String, Participator> map=roomMap.get(roomId);
		map.remove(userId);
		//当房间内的所有成员离开之后，销毁房间
		if(map==null||map.size()==0){
			destroyRoom(roomId);
		}
	}

	/**
	 * 将成员的答题结果加入列表
	 * @param roomId
	 * @param userId
	 * @param result
	 */
	public void addResultRoom(String roomId,String userId,String result){
		Map<String, String> map=roomResultMap.get(roomId);
		if(map==null){
			map=new HashMap<>();
			map.put(userId, result);
			roomResultMap.put(roomId, map);
		}else {
			map.put(userId, result);
		}
	}

	/**
	 * 根据房间号获取该房间的答题结果
	 * @param roomId
	 * @return
	 */
	public Map<String, String> getRoomResult(String roomId){
		return roomResultMap.get(roomId);
	}

	/**
	 * 移除该房间的所有答题结果
	 * @param roomId
	 */
	public void destroyResult(String roomId){
		roomMap.remove(roomId);
		roomResultMap.remove(roomId);
		roomIndexResultMap.remove(roomId);
		roomFinish.remove(roomId);
		roomPrepare.remove(roomId);
	}

	/**
	 * 根据房间号以及题号保存每个人的答题答案
	 * @param roomId
	 * @param index
	 * @param userId
	 * @param answer
	 */
	public void saveIndexResult(String roomId,String index,String userId,String answer){
		Map<String, Map<String, String>> rooMap=roomIndexResultMap.get(roomId);
		if(rooMap==null){
			rooMap=new HashMap<>();
			Map<String, String> indexMap=new HashMap<>();
			indexMap.put(userId, answer);
			rooMap.put(index, indexMap);
		}else{
			Map<String, String> indexMap=rooMap.get(index);
			if(indexMap==null){
				indexMap=new HashMap<>();
				indexMap.put(userId, answer);
			}else {
				indexMap.put(userId, answer);
			}
			rooMap.put(index, indexMap);
		}
		roomIndexResultMap.put(roomId, rooMap);
	}

	/**
	 * 根据题号以及房间号获取已上传的答案
	 * @param roomId
	 * @param index
	 * @return
	 */
	public Map<String, String> getIndexAnswer(String roomId,String index){
		return roomIndexResultMap.get(roomId).get(index);
	}
	/**
	 * 保存答题结束状态
	 * @param roomId
	 * @param userId
	 */
	public void userFinished(String roomId,String userId){
		Map<String, String> finishMap=roomFinish.get(roomId);
		if(finishMap==null){
			finishMap=new HashMap<>();
			finishMap.put(userId, "finish");
			roomFinish.put(roomId, finishMap);
		}else {
			finishMap.put(userId, "finish");
		}
	}

	/**
	 * 返回房间内所有人的完成状态
	 * @param roomId
	 * @return
	 */
	public Map<String, String> getUserFinishState(String roomId){
		return roomFinish.get(roomId);
	}
	/**
	 * 加入准备列表
	 * @param roomId
	 * @param userId
	 * @param socket
	 */
	public void addMapPrepare(String roomId,String userId,Socket socket){
		Map<String, Socket> map=roomPrepare.get(roomId);
		if(map==null){
			map=new HashMap<>();
			map.put(userId, socket);
		}else{
			map.put(userId, socket);
			roomPrepare.put(roomId, map);
		}
		roomPrepare.put(roomId, map);
	}

	/**
	 * 获取所有准备的人
	 * @param roomId
	 * @return
	 */
	public Map<String, Socket> getPrepare(String roomId){
		return roomPrepare.get(roomId);
	}

	public void clearPrepare(String roomid){
		roomPrepare.containsKey(roomid);
	}

}
